package com.example.hzly.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.hzly.common.Result;
import com.example.hzly.dto.DestinationDTO;
import com.example.hzly.service.DestinationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/destinations")
public class DestinationController {

    @Autowired
    private DestinationService destinationService;

    /**
     * 获取目的地详情
     */
    @GetMapping("/{id}")
    public Result<DestinationDTO> getDestinationDetail(@PathVariable Long id) {
        DestinationDTO destination = destinationService.getDestinationDetail(id);
        if (destination == null) {
            return Result.error("目的地不存在");
        }
        return Result.ok(destination);
    }
    /**
     * 根据季节获取列比奥
     */

    /**
     * 获取热门目的地列表
     */
    @GetMapping("/hot")
    public Result<List<DestinationDTO>> getHotDestinations() {
        List<DestinationDTO> destinations = destinationService.getHotDestinations();
        return Result.ok(destinations);
    }

    /**
     * 根据区域获取目的地列表
     */
    @GetMapping("/region/{region}")
    public Result<List<DestinationDTO>> getDestinationsByRegion(@PathVariable String region) {
        List<DestinationDTO> destinations = destinationService.getDestinationsByRegion(region);
        return Result.ok(destinations);
    }

    /**
     * 根据类型获取目的地列表
     */
    @GetMapping("/type/{type}")
    public Result<List<DestinationDTO>> getDestinationsByType(@PathVariable String type) {
        List<DestinationDTO> destinations = destinationService.getDestinationsByType(type);
        return Result.ok(destinations);
    }

    /**
     * 根据季节获取目的地列表
     */
    @GetMapping("/season/{season}")
    public Result<List<DestinationDTO>> getDestinationsBySeason(@PathVariable String season) {
        List<DestinationDTO> destinations = destinationService.getDestinationsBySeason(season);
        return Result.ok(destinations);
    }

    /**
     * 分页查询目的地
     */
    @GetMapping("/page")
    public Result<IPage<DestinationDTO>> pageDestinations(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String region,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) String season,
            @RequestParam(required = false) String theme,
            @RequestParam(required = false, defaultValue = "visits") String orderBy) {
        IPage<DestinationDTO> pageResult = destinationService.pageDestinations(page, size, keyword, region, type, season, theme, orderBy);
        return Result.ok(pageResult);
    }

    /**
     * 获取目的地筛选选项
     */
    @GetMapping("/filter-options")
    public Result<Map<String, Object>> getFilterOptions() {
        Map<String, Object> filterOptions = destinationService.getFilterOptions();
        return Result.ok(filterOptions);
    }

    /**
     * 增加目的地访问量
     */
    @PostMapping("/{id}/increment-visits")
    public Result<Void> incrementVisits(@PathVariable Long id) {
        destinationService.incrementVisits(id);
        return Result.ok();
    }
}